/**
 * Created by ${PRODUCT_NAME}.
 * User: skylerparr
 * Date: 2/1/11
 * Time: 9:18 AM
 * To change this template use File | Settings | File Templates.
 */
package com.thoughtorigin.display.containers {
import flash.display.DisplayObject;
import flash.display.Shape;
import flash.display.Sprite;

import org.flexunit.asserts.assertEquals;
import org.flexunit.asserts.assertNotNull;
import org.flexunit.asserts.assertTrue;
import org.flexunit.async.Async;
import org.mockito.integrations.callOriginal;
import org.mockito.integrations.given;
import org.mockito.integrations.mock;

[RunWith("org.mockito.integrations.flexunit4.MockitoClassRunner")]
[Mock(type="com.thoughtorigin.display.containers.Container")]
public class DefaultMaskContainerTest {

    private var _maskContainer: DefaultMaskContainer;
    private var _container: Container;

    [Before]
    public function setUp(): void {
        _container = new Container();
        _maskContainer = new DefaultMaskContainer(_container);
    }

    [After]
    public function tearDown(): void {
        _maskContainer = null;
        _container = null;
    }

    [Test]
    public function shouldApplyMaskToContainer(): void {
        assertNotNull(_container.mask);
    }

    [Test]
    public function shouldApplyOverridingMask(): void {
        assertNotNull(_container.mask);

        var newMask: Sprite = new Sprite();
        _maskContainer.mask = newMask;
        assertEquals(newMask, _container.mask);
    }

    [Test]
    public function shouldOverrideTheDefaultMaskHeight(): void {
        with(_container.graphics) {
            beginFill(0);
            drawRect(0,0,200,200);
            endFill();
        }
        _maskContainer = new DefaultMaskContainer(_container);
        assertEquals(0, _maskContainer.height);

        _maskContainer.height = 100;
        assertEquals(100, _maskContainer.height);

        assertEquals(100, _container.mask.height);
    }

    [Test]
    public function shouldOverrideTheDefaultMaskWidth(): void {
        with(_container.graphics) {
            beginFill(0);
            drawRect(0,0,200,200);
            endFill();
        }
        _maskContainer = new DefaultMaskContainer(_container);
        assertEquals(0, _maskContainer.width);

        _maskContainer.width = 100;
        assertEquals(100, _maskContainer.width);

        assertEquals(100, _container.mask.width);
    }

    [Test]
    public function shouldSetTheXPosition(): void {
        assertEquals(0, _maskContainer.x);

        _maskContainer.x = 100;
        assertEquals(100, _maskContainer.x);

        assertEquals(100, _container.mask.x);

    }

    [Test]
    public function shouldSetTheYPosition(): void {
        assertEquals(0, _maskContainer.y);

        _maskContainer.y = 100;
        assertEquals(100, _maskContainer.y);

        assertEquals(100, _container.mask.y);

    }

    [Test(async)]
    public function shouldDispatchLayoutRefreshWhenContainerChildrenChange(): void {
        _maskContainer.addEventListener(LayoutEvent.LAYOUT_REFRESH, Async.asyncHandler(this, function(le: LayoutEvent, obj: Object): void {
            assertTrue(true);
        }, 100));

        _container.addChild(getDisplayObject(100, 100));

    }

    [Test(async)]
    public function shouldResetMaskBoundsToMatchContainerBoundsIfNotExplicitlySet(): void {
        var disp: Shape = Shape(getDisplayObject(100, 100));
        _container.addChild(disp);

        _maskContainer.addEventListener(LayoutEvent.LAYOUT_REFRESH, Async.asyncHandler(this, function(le: LayoutEvent, obj: Object): void {
            assertEquals(200, _maskContainer.mask.width);
            assertEquals(300, _maskContainer.mask.height);
        }, 100));

        with(disp.graphics) {
            beginFill(0);
            drawRect(0,0,200, 300);
            endFill();
        }

        _container.recalculateBounds();
    }

    [Test(async)]
    public function shouldNotResetMaskBoundsToMatchContainerBoundsIfExplicitlySet(): void {
        var disp: Shape = Shape(getDisplayObject(100, 100));
        _container.addChild(disp);

        _maskContainer.width = 10;
        _maskContainer.height = 10;

        with(disp.graphics) {
            beginFill(0);
            drawRect(0,0,200, 300);
            endFill();
        }

        _maskContainer.addEventListener(LayoutEvent.LAYOUT_REFRESH, Async.asyncHandler(this, function(le: LayoutEvent, obj: Object): void {
            assertEquals(10, _maskContainer.mask.width);
            assertEquals(10, _maskContainer.mask.height);
        }, 100));


        _container.recalculateBounds();
    }


    private function getDisplayObject(width: Number, height: Number, x: Number = 0, y: Number = 0): DisplayObject {
        var retVal: Shape = new Shape();
        with(retVal.graphics) {
            beginFill(0);
            drawRect(0,0,width, height);
            endFill();
        }
        retVal.x = x;
        retVal.y = y;
        return retVal;
    }
}
}
